from machine import Pin, UART
import micropython
from setcfg import DAT
print("初始化语音串口 tx=%d,rx=%d"%(DAT['yuyin']['tx'],DAT['yuyin']['rx']))
# 初始化UART1
try:
    uart1 = UART(1, baudrate=9600, tx=Pin(DAT['yuyin']['tx']), rx=Pin(DAT['yuyin']['rx']))
except Exception as e:
    print("语音UART初始化失败:", str(e))
    raise

# 定义全局变量
Sustainedmove = 0
Face_Mode = 0
Action_Mode = 0
WeiBa = 0
SpeedDelay = 200
SwingDelay = 6
AllLed = 1  # 开启灯光
BreatheLed = 0  # 开启呼吸灯

#import sys,os
#sys.path.append("oled")
import OLED
import faces

#OLED.init()    
def Face_Config(face_mode):
    # 这里需要实现Face_Config函数的具体逻辑
    face_mode = face_mode % 7  # 防止数组越界    
    OLED.image_direct(faces.Faces[face_mode])

# 删除装饰器
def handle_data(data=None):
    if data is None:
        return
    global Action_Mode, WeiBa, SpeedDelay, SwingDelay, AllLed, BreatheLed
    
    if data == b')':  # 放松的趴下
        Face_Config(0)
        Action_Mode = 0
    elif data == b'0':  # 蹲下
        Face_Config(1)
        Action_Mode = 1
    elif data == b'1':  # 直立
        Face_Config(5)
        Action_Mode = 2
    elif data == b'2':  # 趴下
        Face_Config(1)
        Action_Mode = 3
    elif data == b'3':  # 前进
        Face_Config(2)
        Action_Mode = 4
    elif data == b'4':  # 后退
        Face_Config(2)
        Action_Mode = 5
    elif data == b'5':  # 左转
        Face_Config(2)
        Action_Mode = 6
    elif data == b'6':  # 右转
        Face_Config(2)
        Action_Mode = 7
    elif data == b'7':  # 摇摆
        Face_Config(4)
        Action_Mode = 8
    elif data == b'8':  # 减少移动延迟，增加移动速度
        if SpeedDelay == 120:
            Face_Config(3)
        if SpeedDelay > 100:
            SpeedDelay -= 20
        else:
            Face_Config(2)
            SpeedDelay = 200
    elif data == b'9':  # 减少摇摆延迟，增加摇摆速度
        if SwingDelay == 4:
            Face_Config(3)
        if SwingDelay > 3:
            SwingDelay -= 1
        else:
            Face_Config(4)
            SwingDelay = 9
    elif data == b'@':  # 摇尾巴
        WeiBa = 1 if WeiBa == 0 else 0
        Face_Config(1)
        Action_Mode = 9
    elif data == b'A':  # 向前跳
        Face_Config(2)
        Action_Mode = 10
    elif data == b'B':  # 向后跳
        Face_Config(2)
        Action_Mode = 11
    elif data == b'C':  # 打招呼
        Face_Config(6)
        Action_Mode = 13
    elif data == b'D':  # 开启灯光
        AllLed = 1
    elif data == b'E':  # 关闭灯光
        AllLed = 0
    elif data == b'F':  # 开启呼吸灯
        BreatheLed = 1
    elif data == b'G':  # 关闭呼吸灯
        BreatheLed = 0
    elif data == b'H':  # 伸懒腰
        Face_Config(6)
        Action_Mode = 14
    elif data == b'I':  # 伸懒腰
        Face_Config(6)
        Action_Mode = 15


# 设置UART1中断
def irq_handler(uart):  # 添加uart参数   
    if uart.any():      # 使用参数中的uart对象        
        data = uart.read(1)        
        if data:
            handle_data(data)
print("打开语音中断")
try:
    uart1.irq(trigger=UART.IRQ_RX, handler=irq_handler)  # 直接传递函数引用
except Exception as e:
    print("语音中断设置失败:", str(e))
    raise

